﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Reflection;
using NurseStation.common;
using NurseStation;
using System.Windows.Controls.Primitives;
using NurseStation.views;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System.Windows.Shell;
using Prism.Events;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Unity;
using Prism.Mvvm;
using Prism.Commands;
using Prism.Navigation.Regions;
using MathNet.Numerics.Distributions;
using SqlSugar;
using System.IO;
using NurseStation.DataModels;
using Microsoft.VisualBasic.ApplicationServices;
using NurseStation.Services;
namespace NurseStation.viewmodels
{
    /// <summary>
    /// 批量录入ViewModel
    /// </summary>
    public class BatchVitalSignViewModel : VmBase, INavigationAware
    {

        readonly Notifiaction notifiaction = Notifiaction.Instance;
        NurseDayWorkService nurseDayWorkService = new NurseDayWorkService();

        #region 属性

        /// <summary>
        ///  当前编辑的单元格项目编码 
        /// </summary>
        private NurseDayWork EditObj = new NurseDayWork();

        private bool _CanEditData = true;

        /// <summary>
        /// 是否可修改搜索条件 
        /// </summary>
        public bool CanEditData
        {
            get
            {
                return _CanEditData;
            }
            set
            {
                _CanEditData = value;
                RaisePropertyChanged("CanEditData");
            }
        }

        private DateTime _ChooseDATE;
        /// <summary>
        /// 选择的录入时间  2023-05-24
        /// </summary>
        public DateTime ChooseDATE
        {
            get
            {
                return _ChooseDATE;
            }
            set
            {
                _ChooseDATE = value;
                LoadData();
                RaisePropertyChanged("ChooseDATE");
            }
        }

        private List<NurseDayWork> _AllNurseDayWork;
        /// <summary>
        /// DataGrid绑定列表数据  2023-05-24
        /// </summary>
        public List<NurseDayWork> AllNurseDayWork
        {
            get
            {
                return _AllNurseDayWork;
            }
            set
            {
                _AllNurseDayWork = value;
                RaisePropertyChanged("AllNurseDayWork");
            }
        }
        private NurseDayWork _CurrVitSign;
        /// <summary>
        /// 当前选中列表数据  2023-05-24
        /// </summary>
        public NurseDayWork CurrVitSign
        {
            get
            {
                return _CurrVitSign;
            }
            set
            {
                _CurrVitSign = value;

                RaisePropertyChanged("CurrVitSign");
            }
        }

        //private DataTemplate _BatchVitalSignDataTemplate;
        ///// <summary>
        ///// 批量录入主题模板 
        ///// </summary>
        //public DataTemplate BatchVitalSignDataTemplate
        //{
        //    get => _BatchVitalSignDataTemplate;
        //    set
        //    {
        //        _BatchVitalSignDataTemplate = value;
        //        RaisePropertyChanged(nameof(BatchVitalSignDataTemplate));
        //    }
        //}

        private Visibility _ColSetVis = Visibility.Collapsed;
        /// <summary>
        /// 是否允许修改列
        /// </summary>
        public Visibility ColSetVis
        {
            get
            {
                return _ColSetVis;
            }
            set
            {
                _ColSetVis = value;
                RaisePropertyChanged(nameof(ColSetVis));
            }
        }
        #region 列展示



        private ColShow _C1;
        /// <summary>
        /// 第1列
        /// </summary>
        public ColShow C1
        {
            get
            {
                return _C1;
            }
            set
            {
                _C1 = value;
                RaisePropertyChanged(nameof(C1));
            }
        }

        private ColShow _C2;
        public ColShow C2
        {
            get { return _C2; }
            set { _C2 = value; RaisePropertyChanged(nameof(C2)); }
        }
        private ColShow _C3;
        public ColShow C3
        {
            get { return _C3; }
            set { _C3 = value; RaisePropertyChanged(nameof(C3)); }
        }
        private ColShow _C4;
        public ColShow C4
        {
            get { return _C4; }
            set { _C4 = value; RaisePropertyChanged(nameof(C4)); }
        }
        private ColShow _C5;
        public ColShow C5
        {
            get { return _C5; }
            set { _C5 = value; RaisePropertyChanged(nameof(C5)); }
        }
        private ColShow _C6;
        public ColShow C6
        {
            get { return _C6; }
            set { _C6 = value; RaisePropertyChanged(nameof(C6)); }
        }
        private ColShow _C7;
        public ColShow C7
        {
            get { return _C7; }
            set { _C7 = value; RaisePropertyChanged(nameof(C7)); }
        }
        private ColShow _C8;
        public ColShow C8
        {
            get { return _C8; }
            set { _C8 = value; RaisePropertyChanged(nameof(C8)); }
        }
        private ColShow _C9;
        public ColShow C9
        {
            get { return _C9; }
            set { _C9 = value; RaisePropertyChanged(nameof(C9)); }
        }
        private ColShow _C10;
        public ColShow C10
        {
            get { return _C10; }
            set { _C10 = value; RaisePropertyChanged(nameof(C10)); }
        }
        private ColShow _C11;
        public ColShow C11
        {
            get { return _C11; }
            set { _C11 = value; RaisePropertyChanged(nameof(C11)); }
        }
        private ColShow _C12;
        public ColShow C12
        {
            get { return _C12; }
            set { _C12 = value; RaisePropertyChanged(nameof(C12)); }
        }
        private ColShow _C13;
        public ColShow C13
        {
            get { return _C13; }
            set { _C13 = value; RaisePropertyChanged(nameof(C13)); }
        }
        private ColShow _C14;
        public ColShow C14
        {
            get { return _C14; }
            set { _C14 = value; RaisePropertyChanged(nameof(C14)); }
        }
        private ColShow _C15;
        public ColShow C15
        {
            get { return _C15; }
            set { _C15 = value; RaisePropertyChanged(nameof(C15)); }
        }
        private ColShow _C16;
        public ColShow C16
        {
            get { return _C16; }
            set { _C16 = value; RaisePropertyChanged(nameof(C16)); }
        }
        private ColShow _C17;
        public ColShow C17
        {
            get { return _C17; }
            set { _C17 = value; RaisePropertyChanged(nameof(C17)); }
        }
        private ColShow _C18;
        public ColShow C18
        {
            get { return _C18; }
            set { _C18 = value; RaisePropertyChanged(nameof(C18)); }
        }
        private ColShow _C19;
        public ColShow C19
        {
            get { return _C19; }
            set { _C19 = value; RaisePropertyChanged(nameof(C19)); }
        }
        private ColShow _C20;
        public ColShow C20
        {
            get { return _C20; }
            set { _C20 = value; RaisePropertyChanged(nameof(C20)); }
        }
        private ColShow _C21;
        public ColShow C21
        {
            get { return _C21; }
            set { _C21 = value; RaisePropertyChanged(nameof(C21)); }
        }
        private ColShow _C22;
        public ColShow C22
        {
            get { return _C22; }
            set { _C22 = value; RaisePropertyChanged(nameof(C22)); }
        }
        private ColShow _C23;
        public ColShow C23
        {
            get { return _C23; }
            set { _C23 = value; RaisePropertyChanged(nameof(C23)); }
        }
        private ColShow _C24;
        public ColShow C24
        {
            get { return _C24; }
            set { _C24 = value; RaisePropertyChanged(nameof(C24)); }
        }
        private ColShow _C25;
        public ColShow C25
        {
            get { return _C25; }
            set { _C25 = value; RaisePropertyChanged(nameof(C25)); }
        }

        private ColShow _C26;
        public ColShow C26
        {
            get { return _C26; }
            set { _C26 = value; RaisePropertyChanged(nameof(C26)); }
        }

        private ColShow _C27;
        public ColShow C27
        {
            get { return _C27; }
            set { _C27 = value; RaisePropertyChanged(nameof(C27)); }
        }
        private ColShow _C28;
        public ColShow C28
        {
            get { return _C28; }
            set { _C28 = value; RaisePropertyChanged(nameof(C28)); }
        }
        private ColShow _C29;
        public ColShow C29
        {
            get { return _C29; }
            set { _C29 = value; RaisePropertyChanged(nameof(C29)); }
        }
        private ColShow _C30;
        public ColShow C30
        {
            get { return _C30; }
            set { _C30 = value; RaisePropertyChanged(nameof(C30)); }
        }

        private ColShow _C31;
        public ColShow C31
        {
            get { return _C31; }
            set { _C31 = value; RaisePropertyChanged(nameof(C31)); }
        }
        private ColShow _C32;
        public ColShow C32
        {
            get { return _C32; }
            set { _C32 = value; RaisePropertyChanged(nameof(C32)); }
        }
        private ColShow _C33;
        public ColShow C33
        {
            get { return _C33; }
            set { _C33 = value; RaisePropertyChanged(nameof(C33)); }
        }
        private ColShow _C34;
        public ColShow C34
        {
            get { return _C34; }
            set { _C34 = value; RaisePropertyChanged(nameof(C34)); }
        }

        private ColShow _C35;
        public ColShow C35
        {
            get { return _C35; }
            set { _C35 = value; RaisePropertyChanged(nameof(C35)); }
        }
        private ColShow _C36;
        public ColShow C36
        {
            get { return _C36; }
            set { _C36 = value; RaisePropertyChanged(nameof(C36)); }
        }
        private ColShow _C37;
        public ColShow C37
        {
            get { return _C37; }
            set { _C37 = value; RaisePropertyChanged(nameof(C37)); }
        }

        private ColShow _C38;
        public ColShow C38
        {
            get { return _C38; }
            set { _C38 = value; RaisePropertyChanged(nameof(C38)); }
        }
        private ColShow _C39;
        public ColShow C39
        {
            get { return _C39; }
            set { _C39 = value; RaisePropertyChanged(nameof(C39)); }
        }

        private ColShow _C40;
        public ColShow C40
        {
            get { return _C40; }
            set { _C40 = value; RaisePropertyChanged(nameof(C40)); }
        }
        #endregion


        /// <summary>
        /// 列表控件  2023-05-24
        /// </summary>
        public DataGrid dataGrid { get; set; }


        /// <summary>
        /// 搜索命令  2023-05-24
        /// </summary>
        public DelegateCommand<object> CreateCommand { get; set; }
        /// <summary>
        /// 搜索命令  2023-05-24
        /// </summary>
        public DelegateCommand<object> SerchCommand { get; set; }


        /// 改变搜索条件命令  2023-05-24
        /// </summary>
        public DelegateCommand<object> ChangeCommand { get; set; }

      
        /// <summary>
        /// 导出命令 2022-05-26 
        /// </summary>
        public DelegateCommand<object> OutPutCommad { get; private set; }


        public DelegateCommand<object> OutPutYearCommad { get; private set; }
        #endregion

        #region 方法

        /// <summary>
        /// 构造方法  2023-05-24
        /// </summary>
        /// <param name="container"></param>
        /// <param name="eventAggregator"></param>
        public BatchVitalSignViewModel(IContainerExtension container, IEventAggregator eventAggregator) : base(container, eventAggregator)
        {
            //this.BatchVitalSignDataTemplate = ThemeHelper.GetDataTemplate("BatchVitalSignViewThemes");
            this.SerchCommand = new DelegateCommand<object>(OnSerch);
            this.ChangeCommand = new DelegateCommand<object>(OnChangeCommand);
            this.OutPutCommad = new DelegateCommand<object>(OnOutPut);
            this.CreateCommand = new DelegateCommand<object>(OnCreate);
            this.OutPutYearCommad = new DelegateCommand<object>(OnOutPutYear);
            _ChooseDATE = DateTime.Now;
            RaisePropertyChanged("ChooseDATE");
            if (StringResourceHelper.GetXamlSetting().ColSetEnable)
            {
                ColSetVis = Visibility.Visible;
            }
            //设置列头
            SetCols();
        }

        /// <summary>
        /// 整体初始化  2023-05-24
        /// </summary>
        public void Init()
        {
            dataGrid.BeginningEdit += BeginningEdit;
            dataGrid.CurrentCellChanged += AssociatedObject_CurrentCellChanged;
            dataGrid.CellEditEnding += EndingCellEdit;
            dataGrid.PreviewKeyDown += AssociatedObject_PreviewKeyDown;
            OnSerch(null);
        }

        /// <summary>
        /// 刷新列表  2023-05-24
        /// </summary>
        /// <param name="arg"></param>
        void OnSerch(object arg)
        {
            //var db = new SqlSugarClient(new ConnectionConfig()
            //{
            //    ConnectionString = "Data Source=NurseWork.db;",
            //    DbType = SqlSugar. DbType.Sqlite,
            //    IsAutoCloseConnection = true,
            //    InitKeyType = InitKeyType.Attribute
            //});
            //// 创建表
            //db.CodeFirst.SetStringDefaultLength(255).InitTables(typeof(ColShow));
            //return;
            LoadData();
          
        }

        /// <summary>
        /// 整合数据，实体处理加载  2023-05-24
        /// </summary>
        private void LoadData(bool showMsg = true)
        {
            try
            {
                string GroupName = ChooseDATE.ToString("yyyy-MM");
                Expressionable<NurseDayWork> exp = Expressionable.Create<NurseDayWork>();
                exp.And(it => it.GROUPNAME == GroupName);
                List<NurseDayWork> Copy = nurseDayWorkService.GetListByWhere(exp);
                if (Copy.Count > 0)
                {
                    NurseDayWork sumn = new NurseDayWork();
                    for (int i = Copy.Count - 1; i >= 0; i--)
                    {
                        if (Copy[i].IS_SUM == 1)
                        {
                            Copy.Remove(Copy[i]);
                            continue;
                        }
                        sumn.C1 = (TriageEx.GetNum(sumn.C1) + TriageEx.GetNum(Copy[i].C1)).ToString();
                        sumn.C2 = (TriageEx.GetNum(sumn.C2) + TriageEx.GetNum(Copy[i].C2)).ToString();
                        sumn.C3 = (TriageEx.GetNum(sumn.C3) + TriageEx.GetNum(Copy[i].C3)).ToString();
                        sumn.C4 = (TriageEx.GetNum(sumn.C4) + TriageEx.GetNum(Copy[i].C4)).ToString();
                        sumn.C5 = (TriageEx.GetNum(sumn.C5) + TriageEx.GetNum(Copy[i].C5)).ToString();
                        sumn.C6 = (TriageEx.GetNum(sumn.C6) + TriageEx.GetNum(Copy[i].C6)).ToString();
                        sumn.C7 = (TriageEx.GetNum(sumn.C7) + TriageEx.GetNum(Copy[i].C7)).ToString();
                        sumn.C8 = (TriageEx.GetNum(sumn.C8) + TriageEx.GetNum(Copy[i].C8)).ToString();
                        sumn.C9 = (TriageEx.GetNum(sumn.C9) + TriageEx.GetNum(Copy[i].C9)).ToString();
                        sumn.C10 = (TriageEx.GetNum(sumn.C10) + TriageEx.GetNum(Copy[i].C10)).ToString();
                        sumn.C11 = (TriageEx.GetNum(sumn.C11) + TriageEx.GetNum(Copy[i].C11)).ToString();
                        sumn.C12 = (TriageEx.GetNum(sumn.C12) + TriageEx.GetNum(Copy[i].C12)).ToString();
                        sumn.C13 = (TriageEx.GetNum(sumn.C13) + TriageEx.GetNum(Copy[i].C13)).ToString();
                        sumn.C14 = (TriageEx.GetNum(sumn.C14) + TriageEx.GetNum(Copy[i].C14)).ToString();
                        sumn.C15 = (TriageEx.GetNum(sumn.C15) + TriageEx.GetNum(Copy[i].C15)).ToString();
                        sumn.C16 = (TriageEx.GetNum(sumn.C16) + TriageEx.GetNum(Copy[i].C16)).ToString();
                        sumn.C17 = (TriageEx.GetNum(sumn.C17) + TriageEx.GetNum(Copy[i].C17)).ToString();
                        sumn.C18 = (TriageEx.GetNum(sumn.C18) + TriageEx.GetNum(Copy[i].C18)).ToString();
                        sumn.C19 = (TriageEx.GetNum(sumn.C19) + TriageEx.GetNum(Copy[i].C19)).ToString();
                        sumn.C20 = (TriageEx.GetNum(sumn.C20) + TriageEx.GetNum(Copy[i].C20)).ToString();
                        sumn.C21 = (TriageEx.GetNum(sumn.C21) + TriageEx.GetNum(Copy[i].C21)).ToString();
                        sumn.C22 = (TriageEx.GetNum(sumn.C22) + TriageEx.GetNum(Copy[i].C22)).ToString();
                        sumn.C23 = (TriageEx.GetNum(sumn.C23) + TriageEx.GetNum(Copy[i].C23)).ToString();
                        sumn.C24 = (TriageEx.GetNum(sumn.C24) + TriageEx.GetNum(Copy[i].C24)).ToString();
                        sumn.C25 = (TriageEx.GetNum(sumn.C25) + TriageEx.GetNum(Copy[i].C25)).ToString();
                        sumn.C26 = (TriageEx.GetNum(sumn.C26) + TriageEx.GetNum(Copy[i].C26)).ToString();
                        sumn.C27 = (TriageEx.GetNum(sumn.C27) + TriageEx.GetNum(Copy[i].C27)).ToString();
                        sumn.C28 = (TriageEx.GetNum(sumn.C28) + TriageEx.GetNum(Copy[i].C28)).ToString();
                        sumn.C29 = (TriageEx.GetNum(sumn.C29) + TriageEx.GetNum(Copy[i].C29)).ToString();
                        sumn.C30 = (TriageEx.GetNum(sumn.C30) + TriageEx.GetNum(Copy[i].C30)).ToString();
                        sumn.IS_SUM = 1;
                    }
                    Copy.Add(sumn);
                }
                AllNurseDayWork = Copy;
                if (showMsg)
                    notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "数据已刷新！", NotifiactionType = EnumPromptType.Success });
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error("加载报错：" + ex.ToString());
            }
        }

        #endregion

        #region 命令处理方法

        /// <summary>
        /// 刷新设置所有列的显示
        /// </summary>
        private void SetCols()
        {
            List<ColShow> listCol = nurseDayWorkService.GetDictCol();
            for (int i = 0; i < listCol.Count; i++)
            {
                ColShow colTemp = listCol[i];
                colTemp.IsShow = colTemp.IfShow == "是" ? Visibility.Visible : Visibility.Collapsed;
                string proName = "C" + (i + 1).ToString();
                TriageEx.FindProSetValue(proName, listCol[i], this);
            }
        }

        /// <summary>
        /// 调整列
        /// </summary>
        /// <param name="o"></param>
        private void OnChangeCommand(object o)
        {
            var colSetting = container.Resolve<ColSetting>();
            if (colSetting.ShowDialog() == true)
            {
                SetCols();
                LoadData();
            }
        }

        /// <summary>
        /// 单元格编辑前逻辑处理-记录  2023-05-24
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            if (CurrVitSign == null)
            {
                return;
            }
            EditObj = new NurseDayWork() { ID = CurrVitSign.ID };
            if (e.Column.GetType() == typeof(DataGridTextColumn))
            {
                EditObj.CurEditCol = ((DataGridTextColumn)e.Column).SortMemberPath;
                string res = TriageEx.FindProValue<string>(EditObj.CurEditCol, CurrVitSign);
                EditObj.BeforeText = res;
            }
            else if (e.Column.GetType() == typeof(DataGridTemplateColumn))
            {
                #region 模板列找修改的列名
                try
                {
                    DataGridTemplateColumn templeColumn = (DataGridTemplateColumn)e.Column;
                    FrameworkElement element = templeColumn.GetCellContent(e.Row);
                    object element2 = templeColumn.CellTemplate.FindName("DropTempText", element);
                    if (element2 != null && element2.GetType() == typeof(TextBlock))
                    {
                        TextBlock tb = (TextBlock)element2;
                        System.Windows.Data.BindingExpression kl = tb.GetBindingExpression(TextBlock.TextProperty);
                        Binding hhh = kl.ParentBinding;
                        EditObj.CurEditCol = hhh.Path.Path;
                        EditObj.BeforeText = tb.Text;
                    }
                    else
                    {
                        EditObj.ID = 0;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.GetLogHelper().Error("BeginEdit模板列报错：" + ex);
                }
                #endregion
            }
        }


        /// <summary>
        /// 单元格编辑后逻辑处理-保存  2023-05-24
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EndingCellEdit(object sender, DataGridCellEditEndingEventArgs e)
        {
            try
            {
                if (e.Column == null || EditObj == null || sender == null)
                    return;
                TextBox box = null;
                string EndText = "";
                if (e.Column is DataGridTextColumn)
                {
                    box = (e.EditingElement as TextBox);
                    if (box != null)
                        EndText = box.Text.ToString();
                }
                else if (e.Column is DataGridTemplateColumn)
                {
                    box = MyVisualTreeHelper.FindVisualChild<TextBox>(e.EditingElement);
                    if (box != null)
                        EndText = box.Text.ToString();
                }
                if (!string.IsNullOrEmpty(EndText))
                {
                    int a = 0;
                    if (!int.TryParse(EndText, out a))
                    {
                        notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "录入的不是数字！", NotifiactionType = EnumPromptType.Error });
                        if (box != null)
                        {
                            box.Text = EditObj.BeforeText;
                        }
                        return;
                    }
                }
                if (EditObj.BeforeText == EndText)
                {
                    return;
                }
                TriageEx.FindProSetValue(EditObj.CurEditCol, EndText, EditObj);
                if (nurseDayWorkService.UpdateByEditCol(EditObj))
                {
                    notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "保存成功！", NotifiactionType = EnumPromptType.Success });
                }
                else
                {
                    notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "保存失败！", NotifiactionType = EnumPromptType.Error });
                }
                EditObj = new NurseDayWork() { };
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error("整体录入报错" + ex);
            }
        }


        /// <summary>
        /// 选中的单元格改变  2023-05-24
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AssociatedObject_CurrentCellChanged(object sender, System.EventArgs e)
        {
            try
            {
                var item = dataGrid.CurrentCell;
                if (dataGrid.CurrentItem != null && dataGrid.CurrentItem is NurseDayWork)
                {
                    CurrVitSign = (NurseDayWork)dataGrid.CurrentItem;
                }
                if (item != null)
                {
                    dataGrid.BeginEdit();
                }
            }
            catch (System.Exception ex)
            {
                LogHelper.GetLogHelper().Error(ex);
                return;
            }
        }

        /// <summary>
        /// 按键触发事件  2023-05-24
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AssociatedObject_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                var uie = e.OriginalSource as System.Windows.UIElement;
                switch (e.Key)
                {
                    case Key.Enter:
                        NextCellEdit();
                        break;
                    //case Key.Down:
                    //    CellEditByXY(0, 1);
                    //    break;
                    //case Key.Up:
                    //    CellEditByXY(0, -1);
                    //    break;
                    case Key.Left:
                        CellEditByXY(-1, 0);
                        break;
                    case Key.Right:
                        if (uie is TextBox tb2)
                        {
                            if (tb2.Name == "SSY")
                            {
                                TextBox TbSZY = MyVisualTreeHelper.GetVisualBrother<TextBox>(tb2)?.Find(c => c.Name == "SZY");
                                TbSZY?.Focus();
                            }
                            else
                            {
                                CellEditByXY(1, 0);
                            }
                        }
                        else
                        {
                            CellEditByXY(1, 0);
                        }
                        break;
                    default:
                        return;
                }
                e.Handled = true;
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error("按键处理事件报错" + ex);
            }
        }

        /// <summary>
        /// 跳转编辑下一个单元格  2023-05-24
        /// </summary>
        private void NextCellEdit()
        {
            CellEditByXY(0, 1);
        }

        /// <summary>
        /// 通过上下左右移动选中单元，换行或换列处理  2023-05-24
        /// </summary>
        /// <param name="x">右偏移</param>
        /// <param name="y">下偏移</param>
        private void CellEditByXY(int x = 0, int y = 0)
        {
            int canEditCol = 4;
            if (x != 0 && y != 0)
            {
                return;
            }
            Int32 row = this.dataGrid.Items.IndexOf(this.dataGrid.CurrentItem);
            Int32 Col = this.dataGrid.Columns.IndexOf(this.dataGrid.CurrentColumn);
            if (x > 0)
            {//右
                if (Col >= this.dataGrid.Columns.Count - 1)
                {
                    row += 1;
                    Col = canEditCol;
                }
                else
                {
                    Col += 1;
                }
            }
            if (x < 0)
            {//左
                if (Col == canEditCol)
                {
                    row -= 1;
                    Col = this.dataGrid.Columns.Count - 1;
                }
                else
                {
                    Col -= 1;
                }
            }
            if (y > 0)
            {//下
                if (row >= this.dataGrid.Items.Count - 1)
                {
                    row = 0;
                    Col = Col + 1;
                }
                else
                {
                    row += 1;
                }
            }
            if (y < 0)
            {//上
                if (row == 0)
                {
                    row = this.dataGrid.Items.Count - 1;
                    Col = Col - 1;
                    if (Col < canEditCol)
                    {
                        return;
                    }
                }
                else
                {
                    row -= 1;
                }
            }
            var Nextcell = MyVisualTreeHelper.GetCell(this.dataGrid, row, Col);
            if (Nextcell != null)
            {
                Nextcell.Focus();
                this.dataGrid.BeginEdit();
            }
        }

        /// <summary>
        /// 根据选择月份，补全空日期行
        /// </summary>
        /// <param name="obj"></param>
        public void OnCreate(object obj)
        {
            string GroupName = ChooseDATE.ToString("yyyy-MM");
            Expressionable<NurseDayWork> exp = Expressionable.Create<NurseDayWork>();
            exp.And(it => it.GROUPNAME == GroupName);
            List<NurseDayWork> getList1 = nurseDayWorkService.GetListByWhere(exp);
            List<DateTime> hasTime = getList1.Select(c => c.RECORD_TIME)?.ToList();
            DateTime StartDate = new DateTime(ChooseDATE.Year, ChooseDATE.Month, 1); // 获取当前月的最后一天
            DateTime EndDate = new DateTime(ChooseDATE.Year, ChooseDATE.Month, 1).AddMonths(1).AddDays(-1); // 获取当前月的最后一天
            List<NurseDayWork> tempList = new List<NurseDayWork>();
            int days = (EndDate - StartDate).Days;
            for (int ip = 0; ip <= days; ip++)
            {
                DateTime tempRec = StartDate.AddDays(ip);
                if (!hasTime.Contains(tempRec))
                {
                    NurseDayWork tempRow = new NurseDayWork();
                    tempRow.IS_SUM = 0;
                    tempRow.CREATE_TIME = DateTime.Now;
                    tempRow.RECORD_TIME = tempRec;
                    tempRow.GROUPNAME = GroupName;
                    tempList.Add(tempRow);
                }
            }
            if (tempList.Count > 0)
            {
                List<long> list = nurseDayWorkService.Insertable(tempList);
                if (list.Count > 0)
                {
                    LoadData(false);
                    notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "生成成功！", NotifiactionType = EnumPromptType.Success });
                }
                else
                {
                    notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "生成失败！", NotifiactionType = EnumPromptType.Error });
                }
            }
            else
            {
                notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "当前月份数据无缺失！", NotifiactionType = EnumPromptType.Warn });
            }
        }

        /// <summary>
        /// 导出患者列表数据  2023-05-25 
        /// </summary>
        /// <param name="obj"></param>
        public void OnOutPut(object obj)
        {
            if (dataGrid != null)
            {
                List<ColShow> listCol = nurseDayWorkService.GetDictCol();
                DataTable dt = new DataTable();
                //列添加
                dt.Columns.Add(new DataColumn() { ColumnName = "日 期" });
                for (int i = 0; i < listCol.Count; i++)
                {
                    DataColumn column = new DataColumn();
                    column.ColumnName = listCol[i].Header;
                    dt.Columns.Add(column);
                }
                //行添加
                for (int i = 0; i < AllNurseDayWork.Count; i++)
                {
                    DataRow dr = dt.NewRow();

                    if (AllNurseDayWork[i].IS_SUM == 1)
                    {
                        dr[0] = "合计:";
                    }
                    else
                    {
                        dr[0] = AllNurseDayWork[i].RECORD_TIME.ToString("yyy-MM-dd");
                    }
                    for (int j = 1; j < 25; j++)
                    {
                        dr[j] = TriageEx.FindProValue<string>("C" + j.ToString(), AllNurseDayWork[i]);
                    }
                    dt.Rows.Add(dr);
                }
                System.Windows.Forms.SaveFileDialog dialog1 = new System.Windows.Forms.SaveFileDialog();
                dialog1.FileName = string.Concat(new string[]
                {
                     Environment.GetFolderPath(Environment.SpecialFolder.Desktop),  "\\查询数据", ".xls"
                });
                dialog1.Filter = @"xls files(*.xls)|*.xls";
                if (dialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ExcelHelper.Export(dt, string.Concat(new string[] { "查询数据" }), dialog1.FileName);
                }
                notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "查询数据已导出！", NotifiactionType = EnumPromptType.Success });

            }
        }

        /// <summary>
        /// 统计当年数据
        /// </summary>
        /// <param name="obj"></param>
        public void OnOutPutYear(object obj)
        {
            MessageBoxResult result = MessageBox.Show($"确定要汇总{ChooseDATE.ToString("yyyy")}年的数据吗？", "请确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
                DataForYearViewModel viewModel = new DataForYearViewModel(container, eventAggregator, regionManager);
                viewModel.ChooseDATE = ChooseDATE;
                viewModel.LoadData();
                DataForYear year1 = new DataForYear(viewModel);
                year1.ShowDialog();
            }
        }
        #endregion

        #region 接口实现方法-导航相关

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
        }
        #endregion
    }

}